package com.ssy.lingxi.purchase.controller.tender;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.ExpertExtractBatchQueryRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.ExpertQueryRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.ExpertRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.StatusRequest;
import com.ssy.lingxi.purchase.api.dto.response.tender.ExpertExtractBatchResponse;
import com.ssy.lingxi.purchase.api.dto.response.tender.ExpertResponse;
import com.ssy.lingxi.purchase.entity.tender.Expert;
import com.ssy.lingxi.purchase.service.tender.IExpertService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @menu: 招投标-评标专家库管理
 * 评标专家库管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/3/20
 */
@RestController
@RequestMapping("/purchase/expert")
public class ExpertController extends BaseController {

    @Resource
    private IExpertService expertService;

    /**
     * 查询评标专家库列表
     * @param pageVO 分页实体
     * @param expertQueryRequest 评标专家库
     */
    @RequestMapping(value = "/getExpertList", method = RequestMethod.GET)
    public Wrapper<PageData<ExpertResponse>> getExpertList(PageVO pageVO, ExpertQueryRequest expertQueryRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Expert> result = expertService.getExpertList(sysUser, pageVO, expertQueryRequest);
        List<ExpertResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<ExpertResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询评标专家库列表--按条件抽取
     * @param expertBatchQueryRequestList 评标专家库
     */
    @RequestMapping(value = "/getBatchExpertList", method = RequestMethod.POST)
    public Wrapper<List<ExpertExtractBatchResponse>> getBatchExpertList(@RequestBody List<ExpertExtractBatchQueryRequest> expertBatchQueryRequestList) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        List<Expert> result = expertService.getBatchExpertList(sysUser, expertBatchQueryRequestList);
        List<ExpertExtractBatchResponse> resultList = this.modelMapper.map(result, new TypeToken<List<ExpertExtractBatchResponse>>(){}.getType());
        return Wrapper.success(resultList);
    }

    /**
     * 查询评标专家库信息
     * @param id 评标专家库id
     */
    @RequestMapping(value = "/getExpert", method = RequestMethod.GET)
    public Wrapper<ExpertResponse> getExpert(HttpServletRequest request, @RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        Expert expert = expertService.getExpert(sysUser, id);
        if(expert != null){
            return Wrapper.success(this.modelMapper.map(expert, ExpertResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 新增/修改评标专家库
     * @param expertRequest 评标专家库实体
     */
    @RequestMapping(value = "/saveOrUpdateExpert", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateExpert(HttpServletRequest request, @RequestBody ExpertRequest expertRequest){
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        return Wrapper.success(expertService.saveOrUpdateExpert(sysUser, this.modelMapper.map(expertRequest, Expert.class)));
    }

    /**
     * 修改评标专家库状态
     * @param statusRequest 评标专家库实体
     */
    @RequestMapping(value = "/updateExpertStatus", method = RequestMethod.POST)
    public Wrapper<Boolean> updateExpertStatus(HttpServletRequest request, @RequestBody StatusRequest statusRequest){
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        return Wrapper.success(expertService.updateExpertStatus(sysUser, statusRequest));
    }

    /**
     * 批量删除评标专家库
     * @param simpleIdListRequest
     */
    @RequestMapping(value = "deleteBatchExpert", method = RequestMethod.POST)
    public Wrapper<String> deleteBatchExpert(HttpServletRequest request, @RequestBody SimpleIdListRequest simpleIdListRequest){
        UserLoginCacheDTO sysUser = this.getSysUser(request);
        return Wrapper.success(expertService.deleteBatchExpert(sysUser, simpleIdListRequest.getIdList()));
    }
}
